﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CSett.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System
Imports Microsoft.Win32
Imports System.Reflection

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '


'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '
#If HARDCORE_CUI

Public Class CCuiF

    '// --------------------------------------------------------------------- '
    '// CONSTANTS

    '// the part written at the start of the file
    Const sFSTART As String = _
        "<?xml version=" & """" & "1.0" & """" & "?>" & vbNewLine & _
        "<!--" & vbNewLine & _
        "Warning! Do not edit the contents of this file." & vbNewLine & _
        "If you attempt to edit this file using an XML editor, you could lose customization and migration functionality." & vbNewLine & _
        "If you need to change information in the customization file, use the Customize User Interface dialog box in the product." & vbNewLine & _
        "To access the Customize User Interface dialog box, click the Tools menu > Customize > Interface, or enter CUI on the command line. " & vbNewLine & _
        "-->" & vbNewLine & _
        "<CustSection xmlns:xsi=" & """" & "http://www.w3.org/2001/XMLSchema-instance" & """" & " xmlns:xsd=" & """" & "http://www.w3.org/2001/XMLSchema" & """" & ">" & vbNewLine & _
        "  <FileVersion MajorVersion=" & """" & "0" & """" & " MinorVersion=" & """" & "3" & """" & " IncrementalVersion=" & """" & "2" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "  <Header>" & vbNewLine & _
        "    <CommonConfiguration>" & vbNewLine & _
        "      <CommonItems>" & vbNewLine & _
        "        <ModifiedRev MajorVersion=" & """" & "17" & """" & " MinorVersion=" & """" & "0" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "      </CommonItems>" & vbNewLine & _
        "    </CommonConfiguration>" & vbNewLine & _
        "    <WorkspaceRoot>" & vbNewLine & _
        "      <WorkspaceConfigRoot />" & vbNewLine & _
        "    </WorkspaceRoot>" & vbNewLine & _
        "  </Header>" & vbNewLine & _
        "  <MenuGroup Name=" & """" & "FIELDSK" & """" & ">" & vbNewLine & _
        "    <MacroGroup Name=" & """" & "FieldSK" & """" & ">" & vbNewLine

    '// the part written in the middle, after all macros
    Const sFBEFORETOOLB As String = _
        "    </MacroGroup>" & vbNewLine & _
        "    <MenuRoot>" & vbNewLine & _
        "      <AcceleratorRoot />" & vbNewLine & _
        "      <OverrideRoot />" & vbNewLine & _
        "      <MouseButtonRoot />" & vbNewLine & _
        "      <DigitizerButtonRoot />" & vbNewLine & _
        "      <PopMenuRoot />" & vbNewLine & _
        "      <ToolbarRoot>" & vbNewLine

    '// the part written at the end of the file
    Const sFEND As String = _
        "      </ToolbarRoot>" & vbNewLine & _
        "      <ScreenMenuRoot />" & vbNewLine & _
        "      <ImageMenuRoot />" & vbNewLine & _
        "      <TabletMenuRoot />" & vbNewLine & _
        "      <DoubleClickRoot />" & vbNewLine & _
        "    </MenuRoot>" & vbNewLine & _
        "  </MenuGroup>" & vbNewLine & _
        "</CustSection>" & vbNewLine

    '// menu macro entry
    Const sF_MNMACRO As String = _
        "      <MenuMacro UID=" & """" & "ID_{0}" & """" & ">" & vbNewLine & _
        "        <Macro type=" & """" & "Any" & """" & ">" & vbNewLine & _
        "          <Revision MajorVersion=" & """" & "16" & """" & " MinorVersion=" & """" & "2" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "          <ModifiedRev MajorVersion=" & """" & "17" & """" & " MinorVersion=" & """" & "0" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "          <Name xlate=" & """" & "true" & """" & " UID=" & """" & "XLS_{1}" & """" & ">{0}</Name>" & vbNewLine & _
        "          <Command>^C^C{0}</Command>" & vbNewLine & _
        "          <SmallImage Name=" & """" & "{2}.bmp" & """" & " />" & vbNewLine & _
        "          <LargeImage Name=" & """" & "{2}.bmp" & """" & " />" & vbNewLine & _
        "        </Macro>" & vbNewLine & _
        "      </MenuMacro>" & vbNewLine

    '// starts a toolbar entry
    Const sF_TB_START As String = _
        "        <Toolbar ToolbarOrient=" & """" & "floating" & """" & " ToolbarVisible=" & """" & "show" & """" & " xval=" & """" & "{2}" & """" & " yval=" & """" & "{3}" & """" & " rows=" & """" & "1" & """" & " UID=" & """" & "EID_TNIK_{1}" & """" & ">" & vbNewLine & _
        "          <ModifiedRev MajorVersion=" & """" & "17" & """" & " MinorVersion=" & """" & "0" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "          <Alias>{0}</Alias>" & vbNewLine & _
        "          <Name xlate=" & """" & "true" & """" & " UID=" & """" & "XLS_{4}" & """" & ">{0}</Name>" & vbNewLine
    '// ends a toolbar entry
    Const sF_TB_END As String = "        </Toolbar>" & vbNewLine

    '// toolbar item entry
    Const sF_TB_ENTRY As String = _
        "          <ToolbarButton IsSeparator=" & """" & "false" & """" & " UID=" & """" & "TBBU_{0}" & """" & ">" & vbNewLine & _
        "            <ModifiedRev MajorVersion=" & """" & "17" & """" & " MinorVersion=" & """" & "0" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "            <Name xlate=" & """" & "true" & """" & " UID=" & """" & "XLS_{1}" & """" & ">{2}</Name>" & vbNewLine & _
        "            <MenuItem>" & vbNewLine & _
        "              <MacroRef MenuMacroID=" & """" & "ID_{3}" & """" & " />" & vbNewLine & _
        "            </MenuItem>" & vbNewLine & _
        "          </ToolbarButton>" & vbNewLine

    '// toolbar separator
    Const sF_TB_SEP As String = _
        "          <ToolbarButton IsSeparator=" & """" & "true" & """" & " UID=" & """" & "TBBU_{0}" & """" & ">" & vbNewLine & _
        "            <ModifiedRev MajorVersion=" & """" & "17" & """" & " MinorVersion=" & """" & "0" & """" & " UserVersion=" & """" & "1" & """" & " />" & vbNewLine & _
        "          </ToolbarButton>" & vbNewLine


    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// DATA
		Dim objWRi As System.IO.StreamWriter = Nothing
        Dim myCuiFile as string
        Dim sTBTmp As String
        Dim MacroXLS As Integer
        Dim ToolBXLS As Integer
        Dim TBBtnID As Integer
        Dim bInTb As Boolean
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    '// Prepares the class for writing; also does initial writing
    Public Sub New()

        '// ===================================
        '// open CUI for writing
		Dim sAssemblyPath As String = _
		  System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
		myCuiFile = sAssemblyPath & "\Misc\UI.cui"
	    Try
		    objWRi = New System.IO.StreamWriter(myCuiFile, False)
	    Catch
	    End Try
        If (objWRi Is Nothing) Then
            CMain.AlertUser(Cmain._T(TX.EWRITE) & vbNewLine & myCuiFile)
            Return
        End If
        '// ===================================

        '// Write starting part
        objWRi.Write(sFSTART)
        
        '// cache toolbar data
        sTBTmp = ""

        MacroXLS = 1
        ToolBXLS = 1
        TBBtnID = 1

        bInTb = False
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    '// Flushes any byffers to file and closes the file; after this call the
    '// CUI file is ready to be used
    Public Sub Close()

        If (Not objWRi Is Nothing) Then
            If (bInTb = True) Then
                sTBTmp = sTBTmp & sF_TB_END
            End If

            '// Write header for toolbar
            objWRi.Write(sFBEFORETOOLB)

            '// Write toolbar data cached inside
            objWRi.Write(sTBTmp)
            sTBTmp = ""
            sTBTmp = Nothing

            '// Write ending part
            objWRi.Write(sFEND)

            objWRi.Close()
        Else
            Debug.Print("Attempt to Close without a file")
            Return
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// adds a command in CUI; sCmd is the full name of the command, in the
    '// form ID_TNIK_ABC; sImg - the name of the image associated with
    '// this command; the path and .bmp extension is added by this Sub
    Public Sub AddMacro(ByRef sCmd As String, ByRef sImg As String)
        If (Not objWRi Is Nothing) Then
            Dim sXLS As String = CStr(MacroXLS)
            MacroXLS = MacroXLS + 1
            While sXLS.Length < 4
                sXLS = "0" & sXLS
            End While
            objWRi.Write( _
                String.Format(sF_MNMACRO, _
                    sCmd, sXLS, CApp.sAppPath & "Images\" & sImg))
        Else
            Debug.Print("Attempt to AddMacro without a file")
            Return
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Creates a new toolbar in the file; the path is opened to add buttons
    '// after this call; sName is used to display info to the user and, also
    '// to generate the ID in the form EID_TNIK_Abc
    Public Sub AddToolbar(ByRef sName As String, _
                          ByVal X As Integer, _
                          ByVal Y As Integer)
        If (Not objWRi Is Nothing) Then
            If (bInTb = True) Then
                sTBTmp = sTBTmp & sF_TB_END
            End If

            Dim sXLS As String = CStr(ToolBXLS)
            ToolBXLS = ToolBXLS + 1
            While sXLS.Length < 4
                sXLS = "0" & sXLS
            End While
            Dim sID as String = sName.Replace(" "c, "_"c)
            sTBTmp = sTBTmp & _
                String.Format(sF_TB_START, _
                    sName, sID, X, Y, sXLS)

            bInTb = True
        Else
            Debug.Print("Attempt to AddToolbar without a file")
            Return
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Adds a button to a toolbar; sCmd will be used to identify the command
    Public Sub AddToolbarBtn(ByRef sText As String, ByRef sCmd As String)
        If (Not objWRi Is Nothing) Then
            If (bInTb = False) Then
                Debug.Print("Attempt to add button while not in Toolbar")
                Return
            End If
            Dim sXLS As String = CStr(ToolBXLS)
            ToolBXLS = ToolBXLS + 1
            While sXLS.Length < 4
                sXLS = "0" & sXLS
            End While
            Dim sBTNiD As String = CStr(TBBtnID)
            TBBtnID = TBBtnID + 1
            While sBTNiD.Length < 4
                sBTNiD = "0" & sBTNiD
            End While

            sTBTmp = sTBTmp & _
                String.Format(sF_TB_ENTRY, _
                    sBTNiD, sXLS, sText, sCmd)

        Else
            Debug.Print("Attempt to AddToolbarBtn without a file")
            Return
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Adds a separator to a toolbar
    Public Sub AddToolbarSep()
        If (Not objWRi Is Nothing) Then
            If (bInTb = False) Then
                Debug.Print("Attempt to add separator while not in Toolbar")
                Return
            End If

            Dim sBTNiD As String = CStr(TBBtnID)
            TBBtnID = TBBtnID + 1
            While sBTNiD.Length < 4
                sBTNiD = "0" & sBTNiD
            End While
            sTBTmp = sTBTmp & ""
                String.Format(sF_TB_SEP, _
                    sBTNiD)
        Else
            Debug.Print("Attempt to AddToolbarSep without a file")
            Return
        End If
    End Sub
    '// ===================================================================== '

End Class
#End If

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '



